package com.cardvalue.sys.activity;

import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.cardvalue.sys.R;
import com.cardvalue.sys.adapter.AddressRecylerAdapter;
import com.cardvalue.sys.base.BaseActivity;
import com.cardvalue.sys.entity.Address;
import com.cardvalue.sys.entity.Location;
import com.cardvalue.sys.fragment.BasicFragment;
import com.cardvalue.sys.mode.Service;
import com.cardvalue.sys.mode.WeixinService;
import com.cardvalue.sys.newnetwork.Config;
import com.cardvalue.sys.tool.DividerItemDecoration;
import com.cardvalue.sys.tool.MTextWatcher;
import com.cardvalue.sys.tool.Utiltools;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 搜索地址
 */
public class AddressActivity extends BaseActivity {
    @Bind(R.id.recyclerView) RecyclerView recyclerView; // 登陆按钮
    @Bind(R.id.tv_right)  TextView mSearch;	 // 搜索按钮
    @Bind(R.id.ed_search) EditText mEdSearch; // 搜索输入内容

    private AddressRecylerAdapter mAdapter;
    private WeixinService weixinService ;
    private List<Address> mAddresses= new ArrayList<>();
    private List<Location> mLocation= new ArrayList<>();
    private Service.GetAddress  addressService;
    private Retrofit retrofit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address);
        ButterKnife.bind(this);
        weixinService=new WeixinService(this);
        retrofit = new Retrofit.Builder()   //new出对象
                .baseUrl(Config.getWeixinIp())  //添加url
                .addConverterFactory(GsonConverterFactory.create()) //利用gson解析
                .build();
        //创建一个线性布局管理器
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);

        //设置Item增加、移除动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        //添加分割线
                          /*  recyclerView.addItemDecoration(new DividerItemDecoration(
                                    AddressActivity.this, DividerItemDecoration.HORIZONTAL_LIST));*/
        recyclerView.addItemDecoration(new DividerItemDecoration(AddressActivity.this,LinearLayoutManager.VERTICAL));
        mAdapter=new AddressRecylerAdapter(AddressActivity.this,mLocation);
        recyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickListenter(new AddressRecylerAdapter.OnItemClickListenter() {
            @Override
            public void OnItemClick(View v, int position) {
                Location item  =mLocation.get(position);
                Message ms=new Message();
                Bundle bundle=new Bundle();
                ms.what=1;
                bundle.putString("businessAddress",item.getName().toString());
                bundle.putString("lngAndlat",item.getLngAndlat().toString());
                Utiltools.printE("bizAddrLonlat===",item.getLngAndlat().toString());
                ms.setData(bundle);
                BasicFragment.handler.sendMessage(ms);
                finish();
            }
        });


        mEdSearch.addTextChangedListener(new MTextWatcher(){
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                if(s!=null && s.length()!=0){

                    addressService=retrofit.create(Service.GetAddress.class);
                    Call<Address>  resp=addressService.attemAddress(s.toString());
                    resp.enqueue(new Callback<Address>() {
                        @Override
                        public void onResponse(Call<Address> call, Response<Address> response) {
                            Address address=response.body();
                            Utiltools.printE("code",address.getResultCode()+"ResultCode()");
                            Utiltools.printE("code",address.getError()+"Error()");

                            if(TextUtils.isEmpty(address.getError())){
                                Utiltools.printE("code",address.getError()+"Error555555555()");
                                mLocation=address.getResultData();
                               mAdapter.updateDAta(mLocation);
                            }

                        }

                        @Override
                        public void onFailure(Call<Address> call, Throwable t) {

                        }
                    });
                }
            }
        });
        initData();
    }


    private void initData(){
        String text = getIntent().getStringExtra("text");
        mEdSearch.setText(text);
    }

}
